package com.example.springvue.service.Impl;

import com.example.springvue.common.utils.PageUitl;
import com.example.springvue.mapper.AuthGroupMapper;
import com.example.springvue.model.AuthGroup;
import com.example.springvue.service.AuthGroupService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@Service
public class AuthGroupServiceImpl implements AuthGroupService {
    @Resource
    private AuthGroupMapper authGroupMapper;

    //根据id获取
    @Override
    public AuthGroup getAuthGroupInfoId(int Id) {
        AuthGroup authGroup = authGroupMapper.getAuthGroupInfoId(Id);
        return authGroup;
    }


    //获取列表
    @Override
    public List<Map<String, Object>> getList() {
        List<Map<String,Object>> list = authGroupMapper.getList();
        return list;
    }

    //分页查询
    @Override
    public Map<String, Object> getPageSelect(int pageSize, int page) {
        PageHelper.startPage(page,pageSize);
        List<Map<String ,Object>> authGroup = authGroupMapper.getPageSelect();
        Map<String ,Object> list = PageUitl.page(authGroup,pageSize,page);
        return list;
    }

    //添加
    @Override
    public boolean authGroupAdd(Map<String, Object> map) {
        AuthGroup authRule =new AuthGroup();
        authRule.setName((String) map.get("name"));
        authRule.setRules((String) map.get("rules"));
        authRule.setStatus((Integer) map.get("status"));
        authRule.setSort((Integer) map.get("sort"));
        authRule.setRules((String) map.get("rules"));
        authRule.setCreatetime(new Timestamp(System.currentTimeMillis()));
        authRule.setUpdatetime(new Timestamp(System.currentTimeMillis()));
        int add = authGroupMapper.insert(authRule);
        if (add==1){
            return true;
        }
        return false;
    }

    //修改
    @Override
    public boolean authGroupEdit(Map<String, Object> map) {
        AuthGroup authRule =new AuthGroup();
        authRule.setId((Integer) map.get("id"));
        authRule.setName((String) map.get("name"));
        authRule.setRules((String) map.get("rules"));
        authRule.setStatus((Integer) map.get("status"));
        authRule.setSort((Integer) map.get("sort"));
        authRule.setRules((String) map.get("rules"));
        authRule.setCreatetime(new Timestamp(System.currentTimeMillis()));
        authRule.setUpdatetime(new Timestamp(System.currentTimeMillis()));
        int edit = authGroupMapper.updateById(authRule);
        if (edit==1){
            return true;
        }
        return false;
    }

    //删除
    @Override
    public boolean authGroupDelete(List<String>  id) {
        for (int i = 0; i<id.size();i++){
            String ids = id.get(i);
            Integer Delete = authGroupMapper.authRuleDelete(ids);
        }
        return true;
    }

}
